import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:im_module/common/FriendTypeState.dart';
import 'package:im_module/global_context.dart';
import 'package:im_module/info/UserInfoBodyWdiget.dart';
import 'package:im_module/manager/ImChatManager.dart';
import 'package:im_module/message/ImChatWidget.dart';
import 'package:im_module/model/ImUserInfoBean.dart';
import 'package:im_module/provider/im_provider.dart';
import 'package:im_module/search/SearchFriendWidget.dart';
import 'package:im_module/widget/AppBarBackground.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_user_full_info.dart';

class UserInfoWidget extends StatefulWidget {
  String userId;
  final QueryCallback ageCallBack;

  UserInfoWidget(this.userId, this.ageCallBack);

  @override
  _UserInfoWidgetState createState() => _UserInfoWidgetState();
}

class _UserInfoWidgetState extends State<UserInfoWidget> {
  //默认为-1
  int relation = -1;

  V2TimUserFullInfo? _v2TimUserFullInfo;

  @override
  void initState() {
    super.initState();

    //当如果不是本人，则获取个人信息
    if (widget.userId != ImChatManager().currentUserid) {
      ImChatManager().getFriendsInfo(widget.userId).then((value) {
        setState(() {
          relation = value?.relation ?? -1;
          _v2TimUserFullInfo = value?.friendInfo?.userProfile;
        });
      });
    }
  }

  @override
  void dispose() {
    super.dispose();

    Provider.of<ImProvider>(GlobalContext.getContext(), listen: false)
        .clearQueryUserInfoBean();
  }

  @override
  Widget build(BuildContext context) {
    widget.ageCallBack(2, widget.userId);
    return AppBarBackground(
      child: Scaffold(
        appBar: AppBar(
          title: Text("用户详情"),
          centerTitle: true,
        ),
        body: Container(
          color: Colors.white,
          child: Consumer<ImProvider>(
            builder: (context, ImProvider provider, child) {
              ImUserInfoBean? userInfoBean = provider.userInfoBean;

              if (userInfoBean == null) {
                return Center(
                  child: CupertinoActivityIndicator(),
                );
              }
              return Stack(
                children: [
                  UserInfoBodyWdiget(userInfoBean),
                  Positioned(
                      bottom: 25,
                      child: Container(
                        width: MediaQuery.of(context).size.width,
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        child: getRelation(),
                      )),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  Widget getRelation() {
    if (widget.userId == ImChatManager().currentUserid) {
      return Container();
    }

    if (relation == FriendTypeState.V2TIM_FRIEND_RELATION_TYPE_BOTH_WAY) {
      return buildMenu();
    } else if (relation == -1) {
      return Container();
    } else {
      return ElevatedButton(
        onPressed: () {
          ImChatManager().addFriend(widget.userId).then((value) {
            if (value) {
              Navigator.pop(context);
            }
          });
        },
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Text("添加好友"),
        ),
      );
    }
  }

  Row buildMenu() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              ImChatManager().deleteFromFriendList(widget.userId).then((value) {
                if (value) {
                  Navigator.pop(context, true);
                }
              });
            },
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: Text("删除好友"),
            ),
            style: ElevatedButton.styleFrom(
              primary: Colors.deepOrange,
            ),
          ),
        ),
        SizedBox(
          width: 20,
        ),
        Expanded(
            child: ElevatedButton(
                onPressed: () {
                  if (_v2TimUserFullInfo == null) {
                    return;
                  }
                  var showName = "${_v2TimUserFullInfo?.nickName}";

                  Navigator.pop(context);

                  Navigator.push(context, MaterialPageRoute(builder: (_) {
                    return new ImChatWidget(
                      type: 1,
                      id: widget.userId,
                      niceName: showName, //对方好友备注->对方昵称->对方的 userID
                    );
                  }));
                },
                child: Padding(
                  padding: const EdgeInsets.all(10),
                  child: Text("发送消息"),
                ))),
      ],
    );
  }
}
